import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloWorldServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("初始化");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
       Object username=session.getAttribute("username");
        if(username==null){
            resp.sendRedirect("index.html");
        }else {
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out=resp.getWriter();
            out.println("登录成功，Hello Servlet!"+"欢迎用户："+username);
            out.println("getMethod :"+req.getMethod()+ "<br />");
            out.println("getRequestURI :"+req.getRequestURI()+ "<br />");
            out.println("getQueryString:"+req.getQueryString()+ "<br />");
            out.println("getProtocol :"+req.getProtocol()+ "<br />");
            out.println("getContextPath:"+req.getContextPath()+ "<br />");
            out.println("getPathInfo :"+req.getPathInfo()+ "<br />");
            out.println("getPathTranslated :"+req.getPathTranslated()+ "<br />");
            out.println("getServletPath:"+req.getServletPath()+"<br />");
            out.println("getRemoteAddr :"+req.getRemoteAddr()+"<br />");
            out.println("getRemoteHost :"+req.getRemoteHost()+ "<br />");
            out.println("getRemotePort :"+req.getRemotePort()+ "<br />");
            out.println("getLocalAddr :"+req.getLocalAddr()+ "<br />");
            out.println("getLocalName :"+req.getLocalName()+ "<br />");
            out.println("getLocalPort :"+req.getLocalPort()+ "<br />");
            out.println("getServerName :"+req.getServerName()+ "<br />");
            out.println("getServerPort :"+req.getServerPort()+ "<br />");
            out.println("getScheme :"+req.getScheme()+ "<br />");
            out.println("getRequesturl :"+req.getRequestURL()+ "<br />");
        }

    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
